<?php 
if($_GET['id']>0){
	$sql = "select lowest_rate,member_id from ".TARENTO." where id='{$_GET['id']}'";
	$db->query($sql);
	$de = $db->fetchRow();
	$tpl->assign('id',$_GET['id']);
	$tpl->assign('de',$de);
	$tpl->display('tarento_rate_setting.htm');
	exit;
}

//保存
if($_POST['act']=='edit' && $_POST['id']>0){
	$new_rate = $_POST['lowest_rate'];
	$de['lowest_rate'] = $new_rate;
	//查找最高档金额
	$sql = "select max(price_low) as price_low,max(lowest_rate) as lowest_rate from ".TARENTOBACKRATE." limit 1";
	$db->query($sql);
	$rateInfo = $db->fetchRow();
	
	//查找直系上级最低返利比例
	$memberId = $_POST['member_id'];
	$sql = "select `relations`,`level` from ".TARENTO." where member_id={$memberId}";
	$db->query($sql);
	$relations = $db->fetchRow();
	$sql = "select `lowest_rate` from ".TARENTO." where relations='".str_replace(','.$memberId.',', ',', $relations['relations'])."' and level=".($relations['level']-1);
	$db->query($sql);
	$parent = $db->fetchRow();
	$error = "";
	if(!empty($parent) && $parent['lowest_rate']>0){//不能比上级高
		if($new_rate>$parent['lowest_rate']){
			$error = "不能设置比直接上级比例系数高！";
		}
	}
	
	if($new_rate>$rateInfo['lowest_rate']){//不能比最高档高
		$error = "不能超过最高比例".$rateInfo['lowest_rate'];
	}
	
	if(!empty($error)){
		die(json_encode(array('code'=>1,'msg'=>$error)));
	}
	
	$sql = "update ".TARENTO." set lowest_rate='{$new_rate}' where id={$_POST['id']}";
	$db->query($sql);
	
	die(json_encode(array('code'=>0,'msg'=>'success')));
}
?>